% Name: mask.m
% Created 20/12/2011
% Crown Copyright
%
% Description: Masks a matrix with a circular aperture
%               
% @author Kevin O'Holleran <kevin.o-holleran@npl.co.uk>
%
% @param data mxn matrix
% @param ratio double - determines ratio of half-width to apply eliptical
% mask to. The two radii will be ratio*m/2 and ratio*n/2
% @param invert - boolean that inverts mask, default false.
function [ maskedData ] = mask(data,ratio,invert,quadswap)
    if nargin < 3
        quadswap = false;
        invert = false;
    end
    if nargin < 4
        quadswap = false;
    end
    dims = size(data);
    width = dims(1);
    height = dims(2);
    mask = zeros(width,height);
    a = ratio*width/2;
    b = ratio*height/2;
    for i = 1:width
        xi = i-width/2-1;
        for j = 1:height
            yj = j-height/2-1;
            if (xi^2/a^2+yj^2/b^2)<=1
                mask(i,j) = 1;
            end
        end
    end
    if quadswap
        mask = fftshift(mask);
    end
    if invert
        mask = -(mask-1);
    end
    maskedData = mask.*data;
end

